package com.gitee.smalllog.logback.appender;

import ch.qos.logback.core.UnsynchronizedAppenderBase;
import com.gitee.smalllog.core.model.Settings;
import com.gitee.smalllog.core.model.SmallLogProperties;
import com.gitee.smalllog.core.utils.ErrorReporter;
import com.gitee.smalllog.logback.appender.pub.AbstractSmallLogPublisher;

import java.io.IOException;

public abstract class AbstractLogAppender<T> extends UnsynchronizedAppenderBase<T> {

    protected SmallLogProperties properties;
    protected AbstractSmallLogPublisher<T> publisher;
    protected ErrorReporter errorReporter;
    protected Settings settings;

    public AbstractLogAppender() {
        this.settings = new Settings();
    }

    public AbstractLogAppender(Settings settings){
        this.settings = settings;
    }

    @Override
    protected void append(T eventObject) {
        appendInternal(eventObject);
    }

    @Override
    public void start() {
        super.start();
        try {
            this.publisher = buildElasticsearchPublisher();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected void publishEvent(T eventObject) {
        publisher.addEvent(eventObject);
    }

    protected abstract void appendInternal(T eventObject);

    //VisibleForTesting
    protected abstract AbstractSmallLogPublisher<T> buildElasticsearchPublisher() throws IOException;

    public void setProperties(SmallLogProperties properties) {
        this.properties = properties;
    }


    public void setMaxRetries(int maxRetries) {
        settings.setMaxRetries(maxRetries);
    }


    public void setIncludeCallerData(boolean includeCallerData) {
        settings.setIncludeCallerData(includeCallerData);
    }


    public void setLogsToStderr(boolean logsToStderr) {
        settings.setLogsToStderr(logsToStderr);
    }

    public void setMaxQueueSize(int maxQueueSize) {
        settings.setMaxQueueSize(maxQueueSize);
    }

    public void setIndex(String index) {
        settings.setIndex(index);
    }

    public void setType(String type) {
        settings.setType(type);
    }


    public void setLoggerName(String logger) {
        settings.setLoggerName(logger);
    }

    public void setErrorLoggerName(String logger) {
        settings.setErrorLoggerName(logger);
    }


    public void setRawJsonMessage(boolean rawJsonMessage) {
        settings.setRawJsonMessage(rawJsonMessage);
    }

    public void setIncludeMdc(boolean includeMdc) {
        settings.setIncludeMdc(includeMdc);
    }

    public void setMaxMessageSize(int maxMessageSize) {
        settings.setMaxMessageSize(maxMessageSize);
    }

    public void setAppName(String appName){
        settings.setAppName(appName);
    }

    public void setEvn(String evn){
        settings.setEvn(evn);
    }

    public void setRedisHost(String redisHost){
        settings.setRedisHost(redisHost);
    }

    public void setRedisAuth(String redisAuth){
        settings.setRedisAuth(redisAuth);
    }

    public void setRedisPort(int redisPort){
        settings.setRedisPort(redisPort);
    }

    public void setThreadPoolSize(int threadPoolSize){
        settings.setThreadPoolSize(threadPoolSize);
    }
}
